package com.ruoyi.order.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.IdGenerator;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.order.domain.bo.UserOrderBo;
import com.ruoyi.order.domain.vo.UserOrderVo;
import com.ruoyi.order.domain.UserOrder;
import com.ruoyi.order.mapper.UserOrderMapper;
import com.ruoyi.order.service.IUserOrderService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 用户订单Service业务层处理
 *
 * @author zhi
 * @date 2024-03-31
 */
@RequiredArgsConstructor
@Service
public class UserOrderServiceImpl implements IUserOrderService {

    private final UserOrderMapper baseMapper;

    /**
     * 查询用户订单
     */
    @Override
    public UserOrderVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询用户订单列表
     */
    @Override
    public TableDataInfo<UserOrderVo> queryPageList(UserOrderBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<UserOrder> lqw = buildQueryWrapper(bo);
        lqw.orderByDesc(UserOrder::getCreateTime);
        Page<UserOrderVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询用户订单列表
     */
    @Override
    public List<UserOrderVo> queryList(UserOrderBo bo) {
        LambdaQueryWrapper<UserOrder> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<UserOrder> buildQueryWrapper(UserOrderBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<UserOrder> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getUsername()), UserOrder::getUsername, bo.getUsername());
        lqw.like(StringUtils.isNotBlank(bo.getMobile()), UserOrder::getMobile, bo.getMobile());
        lqw.like(StringUtils.isNotBlank(bo.getAddress()), UserOrder::getAddress, bo.getAddress());
        lqw.like(StringUtils.isNotBlank(bo.getOrderId()), UserOrder::getOrderId, bo.getOrderId());
        lqw.like(StringUtils.isNotBlank(bo.getDescription()), UserOrder::getDescription, bo.getDescription());
        lqw.eq(StringUtils.isNotBlank(bo.getOrderPackage()), UserOrder::getOrderPackage, bo.getOrderPackage());
        lqw.eq(bo.getAmount() != null, UserOrder::getAmount, bo.getAmount());
        lqw.eq(StringUtils.isNotBlank(bo.getCurrency()), UserOrder::getCurrency, bo.getCurrency());
        lqw.eq(StringUtils.isNotBlank(bo.getPayId()), UserOrder::getPayId, bo.getPayId());
        lqw.eq(StringUtils.isNotBlank(bo.getTransNo()), UserOrder::getTransNo, bo.getTransNo());
        lqw.eq(StringUtils.isNotBlank(bo.getPayType()), UserOrder::getPayType, bo.getPayType());
        lqw.eq(StringUtils.isNotBlank(bo.getPayerEmail()), UserOrder::getPayerEmail, bo.getPayerEmail());
        lqw.eq(bo.getPayTime() != null, UserOrder::getPayTime, bo.getPayTime());
        lqw.eq(StringUtils.isNotBlank(bo.getOrderStatus()), UserOrder::getOrderStatus, bo.getOrderStatus());
        return lqw;
    }

    /**
     * 新增用户订单
     */
    @Override
    public Boolean insertByBo(UserOrderBo bo) {
        UserOrder add = BeanUtil.toBean(bo, UserOrder.class);
        validEntityBeforeSave(add);
        add.setOrderId(IdGenerator.genId());
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改用户订单
     */
    @Override
    public Boolean updateByBo(UserOrderBo bo) {
        UserOrder update = BeanUtil.toBean(bo, UserOrder.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(UserOrder entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除用户订单
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
